package com.cashmama.market.api.application.controller.authentication.driver;

import com.cashmama.market.api.application.controller.authentication.AuthenticationBaseHandler;
import com.cashmama.market.api.application.controller.authentication.AuthenticationController;
import com.cashmama.market.api.application.controller.authentication.handler.KycDocumentHandler;
import com.cashmama.market.api.application.controller.authentication.handler.vo.AadhaarCardSaveRequest;
import com.cashmama.market.api.application.controller.authentication.personalInfo.InputContent;
import com.cashmama.market.api.application.controller.authentication.response.UserProfileResponse;
import com.cashmama.market.api.application.controller.authentication.vos.AuthenticationContentVo;
import com.cashmama.market.api.application.controller.authentication.vos.InputParams;
import com.cashmama.market.api.application.enums.AuthenticationTypeEnum;
import com.cashmama.market.api.application.enums.ButtonTextEnum;
import com.cashmama.market.api.application.enums.ParamType;
import com.cashmama.market.api.domain.enums.KycTypeEnum;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.enums.JumpRouterEnum;
import com.qianli.user.enums.UserAuthEnum;
import com.qianli.user.enums.UserAuthStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-23
 * Time: 09:56
 */
@Service
public class DriverAuthenticationHandler extends AuthenticationBaseHandler {

    @Autowired
    private KycDocumentHandler kycDocumentHandler;
    @Autowired
    private ApiUserService apiUserService;

    @PostConstruct
    public void init() {
        AuthenticationController.authenticationBaseHandlerMap.put(AuthenticationTypeEnum.DRIVER.getType(), this);
    }

    @Override
    public AuthenticationContentVo content() {
        AuthenticationContentVo authenticationContentVo = explain(new Driver());
        authenticationContentVo.setButtonText(ButtonTextEnum.SUBMIT.getText());
        authenticationContentVo.setButtonVisibility(true);
        return authenticationContentVo;
    }

    @Override
    public UserProfileResponse save(Map<String, String> inputParams) {
        Driver driver = new Driver();
        getObject(driver, inputParams);
        doSave(driver);
        UserProfileResponse userProfileResponse = new UserProfileResponse()
                .setJumpUrl(JumpRouterEnum.PERSONAL_INFO_BANK.getJumpUrl());
        return userProfileResponse;
    }
    private class Driver {

        @InputContent(paramName = "Card No.", param = "cardNo", inputDesc = "Enter your ID No.", paramType = ParamType.TEXT)
        private InputParams cardNo;

        @InputContent(paramName = "DRIVER LICENSE(Front)", param = "frontImg", paramType = ParamType.POST_PHOTO)
        private InputParams frontImg;


        @InputContent(paramName = "DRIVER LICENSE(Back)", param = "backImg", paramType = ParamType.POST_PHOTO)
        private InputParams backImg;

        @InputContent(paramName = "Selfie", param = "handHoldImg", paramType = ParamType.LIVE_CHECK)
        private InputParams handHoldImg;

        @InputContent(paramName = "Pancard No.", param = "pancardNo", paramType = ParamType.TEXT, inputDesc = "Enter your ID No.")
        private InputParams pancardNo;

        @InputContent(paramName = "Pancard No.", param = "pancardFrontImg", paramType = ParamType.POST_PHOTO)
        private InputParams pancardFrontImg;

        public InputParams getPancardNo() {
            return pancardNo;
        }

        public Driver setPancardNo(InputParams pancardNo) {
            this.pancardNo = pancardNo;
            return this;
        }

        public InputParams getPancardFrontImg() {
            return pancardFrontImg;
        }

        public Driver setPancardFrontImg(InputParams pancardFrontImg) {
            this.pancardFrontImg = pancardFrontImg;
            return this;
        }

        public InputParams getCardNo() {
            return cardNo;
        }

        public Driver setCardNo(InputParams cardNo) {
            this.cardNo = cardNo;
            return this;
        }

        public InputParams getFrontImg() {
            return frontImg;
        }

        public Driver setFrontImg(InputParams frontImg) {
            this.frontImg = frontImg;
            return this;
        }

        public InputParams getBackImg() {
            return backImg;
        }

        public Driver setBackImg(InputParams backImg) {
            this.backImg = backImg;
            return this;
        }

        public InputParams getHandHoldImg() {
            return handHoldImg;
        }

        public Driver setHandHoldImg(InputParams handHoldImg) {
            this.handHoldImg = handHoldImg;
            return this;
        }
    }

    private void doSave(Driver driver) {
        AadhaarCardSaveRequest request = new AadhaarCardSaveRequest()
                .setType(Integer.valueOf(KycTypeEnum.DRIVER_LICENSE.getType()))
                .setCardNo(driver.getCardNo().getInputValue())
                .setCardBackUrl(driver.getBackImg().getInputValue())
                .setCardFrontUrl(driver.getFrontImg().getInputValue())
                .setHandHeldUrl(driver.getHandHoldImg().getInputValue())
                .setPanCardNo(driver.getPancardNo().getInputValue())
                .setPanCardUrl(driver.getPancardFrontImg().getInputValue());
        kycDocumentHandler.kycDocumentHandler(request);
        apiUserService.updateUserAuthStatus(UserAuthStatusEnum.SUCCESS, UserAuthEnum.DRIVER_LICENSE_STATUS, RequestLocalInfo.getCurrentUser().getUserCode());
        apiUserService.updateUserAuthStatus(UserAuthStatusEnum.SUCCESS, UserAuthEnum.PAN_STATUS, RequestLocalInfo.getCurrentUser().getUserCode());
    }

}
